package cn.tedu.vrd.mapper;

import cn.tedu.vrd.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    @Insert("insert into product values (null,#{title},#{author},#{intro},#{url},0,0,null,#{categoryId})")
    void insert(Product product);

//    当查询的表里面的字段名和实体类中的属性名不一致时，需要通过@Result注解写一个对应关系，不然数据查询不到
    @Select("select id,title,author,view_count,like_count,url from product")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    List<Product> select();

    @Delete("delete from product where id=#{id}")
    void delete(int id);

    @Select("select id,title,author,view_count,like_count,url from product where category_id=#{id}")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    List<Product> selectByCid(int cid);

    @Select("select id,title,author,view_count,like_count,url from product where title like concat('%',#{wd},'%')")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    List<Product> selectByWd(String wd);

    @Select("select id,title,intro,url from product order by view_count desc limit 0,4")
    List<Product> selectByView();

    @Select("select id,title,intro,url from product order by like_count desc limit 0,4")
    List<Product> selectByLike();

    @Select("select  id,title,author,intro,url,view_count,like_count,created from product where id=#{id}")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    Product selectById(int id);

    @Update("update product set like_count=like_count+1 where id=#{id}")
    void like(int id);

    @Update("update product set view_count=view_count+1 where id=#{id}")
    void view(int id);
}
